package com.yunda.crypt.security.impl;

import com.yunda.crypt.security.IEncryption;
import com.yunda.crypt.security.util.QueryStrUtil;
import com.yunda.crypt.security.util.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Map;

/**
 * Created by Eenie on 2017/8/24.
 * Email: eeniework@gmail.com
 * Des:
 */

public class SignMd5 implements IEncryption {
    public SignMd5() {
    }

    private String byte2hex(byte[] var1) {
        StringBuilder var2 = new StringBuilder();

        for(int var3 = 0; var3 < var1.length; ++var3) {
            String var4 = Integer.toHexString(var1[var3] & 255);
            if(var4.length() == 1) {
                var2.append("0");
            }

            var2.append(var4.toUpperCase());
        }

        return var2.toString();
    }

    private byte[] encryptMD5(String var1, String var2) {
        try {
            byte[] var5 = MessageDigest.getInstance("MD5").digest(var1.getBytes(var2));
            return var5;
        } catch (GeneralSecurityException var3) {
            throw new RuntimeException(this.getStringFromException(var3, var2));
        } catch (UnsupportedEncodingException var4) {
            throw new RuntimeException(var4);
        }
    }

    private String getStringFromException(Throwable var1, String var2) {
        String var3 = "";
        ByteArrayOutputStream var4 = new ByteArrayOutputStream();
        var1.printStackTrace(new PrintStream(var4));

        String var6;
        try {
            var6 = var4.toString(var2);
        } catch (IOException var5) {
            var6 = var3;
        }

        return var6;
    }

    public String decodeRequest(String var1, String var2, String var3) {
        Map var4 = QueryStrUtil.splitUrlQuery(var1);
        String[] var5 = (String[])var4.keySet().toArray(new String[0]);
        Arrays.sort(var5);
        StringBuilder var6 = new StringBuilder();
        var1 = null;
        int var7 = var5.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            String var9 = var5[var8];
            String var10 = (String)var4.get(var9);

            try {
                var10 = URLDecoder.decode(var10, var3);
            } catch (UnsupportedEncodingException var11) {
                throw new RuntimeException(var11);
            }

            if(var9.equalsIgnoreCase("sign")) {
                var1 = var10;
            } else if(StringUtils.areNotEmpty(new String[]{var9, var10})) {
                var6.append(var9).append(var10);
            }
        }

        if(var2 != null && var2.length() > 0) {
            var6.append(var2);
        }

        var2 = this.byte2hex(this.encryptMD5(var6.toString(), var3));
        if(var2.equals(var1)) {
            var1 = "1";
        } else {
            System.out.println("query:" + var6.toString());
            System.out.println("sign2:" + var2);
            var1 = "0";
        }

        return var1;
    }

    public String decodeResponse(String var1, String var2, String var3) {
        return var1;
    }

    public String encodeRequest(String var1, String var2, String var3) {
        Map var4 = QueryStrUtil.splitUrlQuery(var1);
        String[] var5 = (String[])var4.keySet().toArray(new String[0]);
        Arrays.sort(var5);
        StringBuilder var6 = new StringBuilder();
        int var7 = var5.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            String var9 = var5[var8];
            String var10 = (String)var4.get(var9);

            try {
                var10 = URLDecoder.decode(var10, var3);
            } catch (UnsupportedEncodingException var11) {
                throw new RuntimeException(var11);
            }

            if(StringUtils.areNotEmpty(new String[]{var9, var10})) {
                var6.append(var9).append(var10);
            }
        }

        if(var2 != null && var2.length() > 0) {
            var6.append(var2);
        }

        byte[] var12 = this.encryptMD5(var6.toString(), var3);
        return var1 + "&sign=" + this.byte2hex(var12);
    }

    public String encodeResponse(String var1, String var2, String var3) {
        return var1;
    }

    public String getName() {
        return "yd_md5";
    }
}
